---
title: Civic and Political Engagement during the Multifaceted COVID-19 Crisis
author: "Endre Borbáth, Sophia Hunger, Swen Hutter, Ioana-Elena Oana"
date: "`r format(Sys.time(), '%d %B, %Y')`"
output: 
  html_document:
    toc: true
---

# Figures included in the paper

## Loading the data and recoding

### Loading the data and packages

```{r setup_covid, include=FALSE}
rm(list=ls())
knitr::opts_chunk$set(echo = FALSE,
                      warning = FALSE,
                      message = FALSE,
                      comment=FALSE,
                      cache=FALSE,
                      fig.pos='H',
                      pdf.options(useDingbats = TRUE))

p_needed <- c("haven", "dplyr", "tidyr", "forcats", "ggplot2", "ggrepel", "miceadds", "knitr", "cowplot", 
              "psych", "sandwich", "GPArotation", "texreg")

lapply(p_needed, require, character.only = TRUE)

set.seed(301190)

#path <- "/Users/eborbath/Nextcloud2/Shared/SOLID_paper/Daten/"
path <- "/Users/hunge/Nextcloud/SOLID_paper/Daten/"
dat <- haven::read_dta(paste0(path, "replication_data.dta")) 

```

### Independent variables

```{r recode_demographetc, include=FALSE}

# these are the control variables:

# social structural (age, gender, children, economic situation (cont), education low/tertiary)
# attitudes (interest, left-right, extremes 1-0/9-10) + trust (in the appendix)


dat <- dat %>% 
  mutate(country=as.character(as_factor(country))) %>% 
  mutate(Qcountry=case_when(
         country=="UK" ~ "United Kingdom", 
         country=="France" ~ "France", 
         country=="Germany" ~ "Germany", 
         country=="Italy" ~ "Italy", 
         country=="Spain" ~ "Spain", 
         country=="Sweden" ~ "Sweden", 
         country=="Netherlands" ~ "Netherlands",
         TRUE ~ as.character(NA))) %>% 
  mutate(age=as.numeric(A1)) %>% 
  mutate(age2=age^2) %>% 
  mutate(gender=case_when(
    A2==1 ~ "Male", 
    A2==2 ~ "Female", 
    TRUE ~ as.character(NA))) %>% 
  mutate(gender=factor(gender, levels=c("Male", "Female"))) %>% 
  mutate(children = case_when(
  A7_1 %in% 1 ~ 0,
  A7_1 %in% 2 ~ 1,
  A7_1 %in% 3 ~ 2,
  A7_1 %in% 4 ~ 3,
  A7_1 %in% 5 ~ 4,
  A7_1 %in% 6 ~ 5,
  A7_1 %in% 7 ~ 6,
  A7_1 %in% 98 ~ as.numeric(NA))) %>% 
  mutate(lr_self = ifelse(B11_1 %in% c(98, 99), NA, B11_1)) %>% 
  mutate(extremism= case_when(
    lr_self < 2 ~ 1,
    lr_self > 8 ~ 1,
    TRUE  ~ as.numeric(0))) %>% 
  mutate(extr_left= case_when(
    lr_self < 2 ~ 1,
    lr_self >= 2 | is.na(lr_self) ~ 0)) %>% 
  mutate(extr_right= case_when(
    lr_self > 8 ~ 1,
    lr_self <= 8 | is.na(lr_self) ~ 0)) %>% 
  mutate(pol_intr = case_when(
    B8_1==1 ~ 4,
    B8_1==2 ~ 3,
    B8_1==3 ~ 2,
    B8_1==4 ~ 1,
    TRUE ~ as.numeric(NA))) %>% 
  mutate(edu=case_when(
    education %in% c(1,2) ~ "lower than tertiary",
    education==3 ~ "tertiary",
    TRUE ~ as.character(NA)  
  )) %>% 
  mutate(gov_sat=ifelse(B4_3_2==99, NA, B4_3_2)) %>% 
  mutate(econ_sit=ifelse(A5_1==99, NA, A5_1)) %>% 
  mutate(tr_nat=ifelse(B10_1_2 == 99, NA, B10_1_2)) %>% 
  mutate_at(vars(extremism, gender, edu, Qcountry), ~as.factor(.)) %>% 
  mutate_at(vars(pol_intr, lr_self, tr_nat, econ_sit, gov_sat), ~as.numeric(.))

```

### Key independent variables

```{r recode_IVs, include=FALSE}

dat <- dat %>% 
  mutate(eco_threat_indiv=as.numeric(B1_1_4),
         health_threat_indiv = as.numeric(B1_1_2),
         eco_threat_soc = as.numeric(B1_1_3),
         health_threat_soc = as.numeric(B1_1_1)) %>% 
  mutate(eco_soc_dummy=ifelse(eco_threat_soc>5, 1, 0),
         health_soc_dummy=ifelse(health_threat_soc>5, 1, 0),
         eco_ind_dummy=ifelse(eco_threat_indiv>5, 1, 0),
         health_ind_dummy=ifelse(health_threat_indiv>5, 1, 0),) %>% 
  mutate_at(vars(eco_threat_indiv, health_threat_indiv,
                 eco_threat_soc, health_threat_soc), ~as.numeric(.)) %>% 
  mutate_at(vars(health_soc_dummy, eco_soc_dummy, 
                 health_ind_dummy, eco_ind_dummy), ~as.factor(.))

dat$gov_dissat <- car::recode(dat$gov_sat, '0=10; 1=9; 2=8; 3=7; 4=6; 5=5; 6=4; 7=3; 8=2; 9=1; 10=0')  

```

### Dependendent variables

```{r, warning=FALSE, echo=FALSE, message=FALSE}

# Dependent variable coded in:
# B9_1_1 B9_1_2 B9_1_3 B9_1_4 B9_1_5 B9_1_6 B9_1_7

dat <- dat %>% 
  mutate_at(vars(B9_1_1, B9_1_2, B9_1_3, B9_1_4, B9_1_5, B9_1_6, B9_1_7), ~ ifelse(.==99, NA, .)) %>% 
  mutate_at(vars(B9_1_1, B9_1_2, B9_1_3, B9_1_4, B9_1_5, B9_1_6, B9_1_7), ~ as.numeric(.)) %>% 
  mutate(help_neighborhood = B9_1_1,
         donated_money = B9_1_2,
         signed_petition = B9_1_3,
         contacted_pol = B9_1_4,
         posted_stg = B9_1_5,
         public_demo = B9_1_6,
         stg_else_pol = B9_1_7) 

dat <- dat %>% mutate(public_demo=recode(public_demo, 
                         `1`=0,
                         `2`=1, 
                         `3`=2,
                         `4`=3),
                        donated_money=recode(donated_money,
                         `1`=0,
                         `2`=1, 
                         `3`=2,
                         `4`=3),
                        signed_petition=recode(signed_petition,
                         `1`=0,
                         `2`=1, 
                         `3`=2,
                         `4`=3),
                        contacted_pol=recode(contacted_pol,
                         `1`=0,
                         `2`=1, 
                         `3`=2,
                         `4`=3),
                        help_neighborhood=recode(help_neighborhood,
                         `1`=0,
                         `2`=1, 
                         `3`=2,
                         `4`=3),
                        posted_stg=recode(posted_stg,
                         `1`=0,
                         `2`=1, 
                         `3`=2,
                         `4`=3),
                        stg_else_pol=recode(stg_else_pol,
                         `1`=0,
                         `2`=1, 
                         `3`=2,
                         `4`=3))


dat <- dat %>%
  mutate(demo = if_else(public_demo>0, 1, 0), 
         pol_low = if_else((signed_petition > 0 | contacted_pol > 0| posted_stg > 0), 1, 0),
         soc_eng = if_else((donated_money > 0 | help_neighborhood > 0), 1, 0),
         demo_core = if_else(public_demo==3, 1, 0), 
         pol_low_core = if_else((signed_petition==3 | contacted_pol==3 | posted_stg==3), 1, 0),
         soc_eng_core = if_else((donated_money==3 | help_neighborhood==3), 1, 0)) %>% 
  mutate_at(vars(demo, pol_low, soc_eng, demo_core, pol_low_core, soc_eng_core), ~as.factor(.))
```

### Figure 1

```{r, warning=FALSE, echo=FALSE, message=FALSE, fig.width=7, fig.height=7}

DV_overall <- dat %>% 
  dplyr::select("public_demo", "donated_money", 
                "signed_petition", "contacted_pol",
                "help_neighborhood", "posted_stg", 
                "stg_else_pol", "Qcountry", "weight") %>% 
  group_by(Qcountry) %>% 
  mutate_at(vars("public_demo", "donated_money", 
                 "signed_petition", "contacted_pol",
                 "help_neighborhood", "posted_stg", 
                 "stg_else_pol"), ~ifelse(.>0, 1, 0)) %>% 
  mutate_at(vars("public_demo", "donated_money", 
                 "signed_petition", "contacted_pol",
                 "help_neighborhood", "posted_stg", 
                 "stg_else_pol"), ~.*weight) %>% 
  group_by(Qcountry) %>% 
  mutate_at(vars("public_demo", "donated_money", 
                 "signed_petition", "contacted_pol",
                 "help_neighborhood", "posted_stg", 
                 "stg_else_pol", "weight"), ~sum(., na.rm = TRUE)) %>% 
  unique(.) %>% 
  mutate_at(vars("public_demo", "donated_money", 
                 "signed_petition", "contacted_pol",
                 "help_neighborhood", "posted_stg", 
                 "stg_else_pol"), ~.*100/weight) %>% 
  dplyr::select(-weight) %>% 
  pivot_longer(cols=c("public_demo", "donated_money", 
                      "signed_petition", "contacted_pol",
                      "help_neighborhood", "posted_stg", 
                      "stg_else_pol"), names_to="indicator", values_to="value") %>% 
  mutate(indicator=dplyr::case_when(indicator=="contacted_pol" ~ "Contacted a pol.",
                                    indicator=="donated_money" ~  "Donated money",
                                    indicator=="help_neighborhood" ~ "Help in neighborhood",
                                    indicator=="posted_stg" ~  "Posted on the Internet",
                                    indicator=="public_demo" ~  "Public demonstration",
                                    indicator=="signed_petition" ~  "Signed a petition",
                                    indicator=="stg_else_pol" ~ "Other political")) %>% 
  mutate(indicator=factor(indicator, levels = c("Help in neighborhood", 
                                                "Donated money", "Posted on the Internet", 
                                                "Signed a petition", "Contacted a pol.",  
                                                "Public demonstration", "Other political"))) %>% 
  arrange(Qcountry, indicator) %>% 
  mutate(Qcountry=case_when(Qcountry=="UK" ~ "United Kingdom",
                            Qcountry=="NL" ~ "Netherlands",
                            TRUE ~ as.character(Qcountry))) %>% 
  dplyr::rename(overall=value)

DV_core <- dat %>% 
  dplyr::select("public_demo", "donated_money", 
                "signed_petition", "contacted_pol",
                "help_neighborhood", "posted_stg", 
                "stg_else_pol", "Qcountry", "weight") %>% 
  group_by(Qcountry) %>% 
  mutate_at(vars("public_demo", "donated_money", 
                 "signed_petition", "contacted_pol",
                 "help_neighborhood", "posted_stg", 
                 "stg_else_pol"), ~ifelse(.==3, 1, 0)) %>% 
  mutate_at(vars("public_demo", "donated_money", 
                 "signed_petition", "contacted_pol",
                 "help_neighborhood", "posted_stg", 
                 "stg_else_pol"), ~.*weight) %>% 
  group_by(Qcountry) %>% 
  mutate_at(vars("public_demo", "donated_money", 
                 "signed_petition", "contacted_pol",
                 "help_neighborhood", "posted_stg", 
                 "stg_else_pol", "weight"), ~sum(., na.rm = TRUE)) %>% 
  unique(.) %>% 
  mutate_at(vars("public_demo", "donated_money", 
                 "signed_petition", "contacted_pol",
                 "help_neighborhood", "posted_stg", 
                 "stg_else_pol"), ~.*100/weight) %>% 
  dplyr::select(-weight) %>% 
  pivot_longer(cols=c("public_demo", "donated_money", 
                      "signed_petition", "contacted_pol",
                      "help_neighborhood", "posted_stg", 
                      "stg_else_pol"), names_to="indicator", values_to="value") %>% 
  mutate(indicator=dplyr::case_when(indicator=="contacted_pol" ~ "Contacted a pol.",
                                    indicator=="donated_money" ~  "Donated money",
                                    indicator=="help_neighborhood" ~ "Help in neighborhood",
                                    indicator=="posted_stg" ~  "Posted on the Internet",
                                    indicator=="public_demo" ~  "Public demonstration",
                                    indicator=="signed_petition" ~  "Signed a petition",
                                    indicator=="stg_else_pol" ~ "Other political")) %>% 
  mutate(indicator=factor(indicator, levels = c("Help in neighborhood", 
                                                "Donated money", "Posted on the Internet", 
                                                "Signed a petition", "Contacted a pol.",  
                                                "Public demonstration", "Other political"))) %>% 
  arrange(Qcountry, indicator) %>% 
  mutate(Qcountry=case_when(Qcountry=="UK" ~ "United Kingdom",
                            Qcountry=="NL" ~ "Netherlands",
                            TRUE ~ as.character(Qcountry))) %>% 
  dplyr::rename(core=value)

DV_dummy <- merge(DV_overall, DV_core)

rm(DV_overall, DV_core)

DV_dummy <- DV_dummy %>% 
  ungroup(.) %>% 
  dplyr::select(-Qcountry) %>% 
  group_by(indicator) %>% 
  mutate(overall=mean(overall),
         core=mean(core)) %>% 
  unique(.)

plot1 <- ggplot(data=DV_dummy, aes(x=indicator, y=overall)) +
  geom_bar(stat="identity", position = "dodge", color="black") +
  ylab("Share (percentage points)") +
  scale_y_continuous(breaks = seq(0, 43, by = 10)) +
  ggtitle("Overall Engagement") +
  theme_bw() +
  theme(legend.title=element_blank(), legend.position="bottom") +
  theme(axis.title.x=element_blank(),
        axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1))

plot2 <- ggplot(data=DV_dummy, aes(x=indicator, y=core)) +
  geom_bar(stat="identity", position = "dodge", color="black") +
  scale_y_continuous(limits=c(0, 10.8), breaks = seq(0, 13, by = 5)) +
  ggtitle("Regular Engagement") +
  theme_bw() +
  theme(legend.title=element_blank(), legend.position="bottom") +
  theme(axis.title.x=element_blank(),
        axis.title.y=element_blank(),
        axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1))

plot_comb1 <-cowplot::plot_grid(plot1+theme(legend.position="none"), plot2+theme(legend.position="none"), nrow=1)

plot_comb1

```

### Figure 2

```{r, warning=FALSE, echo=FALSE, message=FALSE, fig.width=7, fig.height=7}

DV_dummy <- dat %>% 
  dplyr::select(demo, pol_low, soc_eng, demo_core, pol_low_core, soc_eng_core, "Qcountry", "weight") %>% 
  # filter(!is.na(.)) %>% 
  group_by(Qcountry) %>% 
  mutate_at(vars(demo, pol_low, soc_eng, demo_core, pol_low_core, soc_eng_core), 
            ~as.numeric(as.character(.))*weight) %>% 
  mutate_at(vars(demo, pol_low, soc_eng, demo_core, pol_low_core, soc_eng_core, weight), ~sum(., na.rm = TRUE)) %>% 
  unique(.) %>% 
  mutate_at(vars(demo, pol_low, soc_eng, demo_core, pol_low_core, soc_eng_core), ~.*100/weight) %>% 
  dplyr::select(-weight) %>% 
  pivot_longer(cols=c(demo, pol_low, soc_eng, demo_core, pol_low_core, soc_eng_core), names_to="indicator", values_to="value") %>% 
  mutate(type=ifelse(grepl("_core", indicator), "Regular participants", "Overall")) %>% 
  mutate(type=factor(type, levels=c("Overall", "Regular participants"))) %>% 
  mutate(form = case_when(
    grepl("demo", indicator) ~ "Demonstrations",
    grepl("pol_low", indicator) ~ "Political engagement",
    grepl("soc_eng", indicator) ~ "Civic engagement")) %>% 
  mutate(form=factor(form, levels=c("Civic engagement", "Political engagement", "Demonstrations"))) %>% 
  dplyr::rename(shares=value) %>% 
  mutate(order=case_when(
    Qcountry=="France" ~ 6,
Qcountry=="Germany" ~ 3,
Qcountry=="Italy" ~ 5,
Qcountry=="Netherlands" ~ 2,
Qcountry=="Spain " ~ 7,
Qcountry=="Sweden" ~ 1,
Qcountry=="United Kingdom" ~ 4))

DV_dummy_wide <- DV_dummy %>% 
  dplyr::select(Qcountry, type, form, shares) %>% 
  pivot_wider(names_from=form, values_from=shares)

plot_scatter <- ggplot(data=DV_dummy_wide[DV_dummy_wide$type=="Overall", ], 
       aes(x=`Political engagement`, y=`Civic engagement`)) +
  geom_smooth(method='lm', formula= y~x, se=FALSE, color="gray60") +
  geom_point(aes(size=`Demonstrations`)) +
  geom_label_repel(aes(label=Qcountry)) +
  xlab("Political engagement (percentage points)") + ylab("Civic engagement (percentage points)") +
  theme_bw() +
  labs(size = "Participation in demonstrations (percentage points):") +
  theme(legend.position="bottom")

plot_scatter 

```

### Figure 3

```{r}
plot1 <- dat %>% 
   count(eco_threat_indiv, eco_threat_soc) %>%  
   ggplot(mapping = aes(eco_threat_indiv, eco_threat_soc)) +
    geom_tile(mapping = aes(fill = n), colour = "black") +
   scale_fill_gradient(low = "gray98", high = "black") + 
  theme_minimal() +
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(), panel.background = element_blank()) + 
   xlab("Perceived egotropic economic threat") + ylab("Perceived sociotropic economic threat")

plot2 <- dat %>% 
  count(health_threat_indiv, health_threat_soc) %>%  
  ggplot(mapping = aes(health_threat_indiv, health_threat_soc)) +
    geom_tile(mapping = aes(fill = n), colour = "black") +
   scale_fill_gradient(low = "gray98", high = "black") + 
  theme_minimal() +
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(), panel.background = element_blank()) + 
   xlab("Perceived egotropic health threat") + ylab("Perceived sociotropic health threat")


plot3 <- cowplot::plot_grid(plot1+theme(legend.position="none"), plot2+theme(legend.position="none"))


legend <- get_legend(
  # create some space to the left of the legend
  plot1 + theme(    guides(color = guide_legend(nrow = 1)) +
    theme(legend.position = "bottom"))
)

cowplot::plot_grid(plot3, legend, rel_widths = c(6, .5), label_size = 12)
```


## Regression analysis

### dropping missing values

```{r, warning=FALSE, echo=FALSE, message=FALSE}
eff_sample <- dat %>% 
  dplyr::select(age, age2, gender, children, edu, 
                econ_sit, extr_left, extr_right, pol_intr, tr_nat, Qcountry,
                 demo, pol_low, soc_eng, demo_core, pol_low_core, soc_eng_core, weight, 
                 eco_threat_indiv, health_threat_indiv,
                 health_soc_dummy, eco_soc_dummy,
                 health_ind_dummy, eco_ind_dummy,
                 gov_dissat) %>% 
  mutate_at(vars(demo, pol_low, soc_eng, demo_core, pol_low_core, soc_eng_core), ~as.numeric(as.character(.))) %>% 
  mutate_at(vars(extr_left, extr_right), ~as.factor(.)) %>% 
  filter(complete.cases(.)) %>% 
  ungroup(.)

# change reference category 

eff_sample$Qcountry = relevel(eff_sample$Qcountry, ref="Spain")
# 
# print(qwraps2::summary_table(eff_sample))

```

### Figure 4

```{r, warning=FALSE, echo=FALSE, message=FALSE, include=FALSE}

# To extract the coefficient:

models <- data.frame(type = c("demo", "pol_low", "soc_eng",
                             "demo_core", "pol_low_core", "soc_eng_core"))

  controls <- c("age", "age2", "gender", "children", "edu", "econ_sit", "extr_left",
                "extr_right", "pol_intr", "tr_nat", "gov_dissat", "Qcountry")
  threats <- c("eco_ind_dummy", "health_ind_dummy", "eco_soc_dummy", "health_soc_dummy")
  
  demo <- as.formula(paste0("demo ~", paste(controls, "+", threats, collapse = "+")))
  pol_low <- as.formula(paste0("pol_low ~", paste(controls, "+", threats, collapse = "+")))
  soc_eng <- as.formula(paste0("soc_eng ~", paste(controls, "+", threats, collapse = "+")))
  demo_core <- as.formula(paste0("demo_core ~", paste(controls, "+", threats, collapse = "+")))
  pol_low_core <- as.formula(paste0("pol_low_core ~", paste(controls, "+", threats, collapse = "+")))
  soc_eng_core <- as.formula(paste0("soc_eng_core ~", paste(controls, "+", threats, collapse = "+")))
  
  mod1 <- glm.cluster(formula = demo,
              data=eff_sample, weights=eff_sample$weight, cluster='Qcountry')
  mod2 <- glm.cluster(formula = pol_low,
              data=eff_sample, weights=eff_sample$weight, cluster='Qcountry')
  mod3 <- glm.cluster(formula = soc_eng,
              data=eff_sample, weights=eff_sample$weight, cluster='Qcountry')
  mod4 <- glm.cluster(formula = demo_core,
              data=eff_sample, weights=eff_sample$weight, cluster='Qcountry')
  mod5 <- glm.cluster(formula = pol_low_core,
              data=eff_sample, weights=eff_sample$weight, cluster='Qcountry')
  mod6 <- glm.cluster(formula = soc_eng_core,
              data=eff_sample, weights=eff_sample$weight, cluster='Qcountry')
  
  mod1_res <- as.data.frame(summary(mod1))
  mod1_res$term <- rownames(mod1_res)
  mod1_res$type <- "demo"
  mod_1_ci <- as.data.frame(confint(mod1))
  mod_1_ci$term <- rownames(mod_1_ci)
  mod1_res <- merge(mod_1_ci, mod1_res, by="term", all=TRUE)
  mod_1_ci <- as.data.frame(confint(mod1, level=0.9))
  mod_1_ci$term <- rownames(mod_1_ci)
  mod1_res <- merge(mod_1_ci, mod1_res, by="term", all=TRUE)

  mod2_res <- as.data.frame(summary(mod2))
  mod2_res$term <- rownames(mod2_res)
  mod2_res$type <- "pol_low"
  mod_2_ci <- as.data.frame(confint(mod2))
  mod_2_ci$term <- rownames(mod_2_ci)
  mod2_res <- merge(mod_2_ci, mod2_res, by="term", all=TRUE)
  mod_2_ci <- as.data.frame(confint(mod2, level=0.9))
  mod_2_ci$term <- rownames(mod_2_ci)
  mod2_res <- merge(mod_2_ci, mod2_res, by="term", all=TRUE)

  mod3_res <- as.data.frame(summary(mod3))
  mod3_res$term <- rownames(mod3_res)
  mod3_res$type <- "soc_eng"
  mod_3_ci <- as.data.frame(confint(mod3))
  mod_3_ci$term <- rownames(mod_3_ci)
  mod3_res <- merge(mod_3_ci, mod3_res, by="term", all=TRUE)
  mod_3_ci <- as.data.frame(confint(mod3, level=0.9))
  mod_3_ci$term <- rownames(mod_3_ci)
  mod3_res <- merge(mod_3_ci, mod3_res, by="term", all=TRUE)

  mod4_res <- as.data.frame(summary(mod4))
  mod4_res$term <- rownames(mod4_res)
  mod4_res$type <- "demo_core"
  mod_4_ci <- as.data.frame(confint(mod4))
  mod_4_ci$term <- rownames(mod_4_ci)
  mod4_res <- merge(mod_4_ci, mod4_res, by="term", all=TRUE)
  mod_4_ci <- as.data.frame(confint(mod4, level=0.9))
  mod_4_ci$term <- rownames(mod_4_ci)
  mod4_res <- merge(mod_4_ci, mod4_res, by="term", all=TRUE)

  mod5_res <- as.data.frame(summary(mod5))
  mod5_res$term <- rownames(mod5_res)
  mod5_res$type <- "pol_low_core"
  mod_5_ci <- as.data.frame(confint(mod5))
  mod_5_ci$term <- rownames(mod_5_ci)
  mod5_res <- merge(mod_5_ci, mod5_res, by="term", all=TRUE)
  mod_5_ci <- as.data.frame(confint(mod5, level=0.9))
  mod_5_ci$term <- rownames(mod_5_ci)
  mod5_res <- merge(mod_5_ci, mod5_res, by="term", all=TRUE)

  mod6_res <- as.data.frame(summary(mod6))
  mod6_res$term <- rownames(mod6_res)
  mod6_res$type <- "soc_eng_core"
  mod_6_ci <- as.data.frame(confint(mod6))
  mod_6_ci$term <- rownames(mod_6_ci)
  mod6_res <- merge(mod_6_ci, mod6_res, by="term", all=TRUE)
  mod_6_ci <- as.data.frame(confint(mod6, level=0.9))
  mod_6_ci$term <- rownames(mod_6_ci)
  mod6_res <- merge(mod_6_ci, mod6_res, by="term", all=TRUE)
  
  models <- merge(models, mod1_res, all=TRUE)
  models <- merge(models, mod2_res, all=TRUE)
  models <- merge(models, mod3_res, all=TRUE)
  models <- merge(models, mod4_res, all=TRUE)
  models <- merge(models, mod5_res, all=TRUE)
  models <- merge(models, mod6_res, all=TRUE)

plot_dat <- models %>% 
  dplyr::filter(term %in% c("eco_ind_dummy1", "eco_soc_dummy1",
                            "health_ind_dummy1", "health_soc_dummy1",
                            "extr_left1", "extr_right1", "age", "age2")) %>% 
  mutate(odds = exp(Estimate),
         odds.outter.upper = exp(`2.5 %`),
         odds.outter.lower = exp(`97.5 %`),
         odds.inner.upper = exp(`5 %`),
         odds.inner.lower = exp(`95 %`),
         p.value=as.factor(ifelse(`Pr(>|t|)`<=0.05, 1, 0)),
         probability = odds/(1+odds),
         prob.outter.upper = odds.outter.upper/(1+odds.outter.upper),
         prob.outter.lower = odds.outter.lower/(1+odds.outter.lower),
         prob.inner.upper = odds.inner.upper/(1+odds.inner.upper),
         prob.inner.lower = odds.inner.lower/(1+odds.inner.lower)) %>% 
  mutate(term=case_when(term=="extr_right1" ~ "Extreme Right",
                        term=="extr_left1" ~ "Extreme Left",
                        term %in% c("eco_ind_dummy1", 
                                    "eco_threat_indiv") ~ "Egotropic economic threat",
                        term %in% c("health_ind_dummy1",
                                    "health_threat_indiv") ~ "Egotropic health threat",
         term=="eco_soc_dummy1" ~ "Sociotropic economic threat",
         term=="health_soc_dummy1" ~ "Sociotropic health threat",
         term=="age" ~ "Threats",
         term=="age2" ~ "Ideology")) %>% 
  mutate(typeDV=case_when(
    type %in% c("demo", "demo_core") ~ "Demonstrations",
    type %in% c("pol_low", "pol_low_core") ~ "Political Eng.",
    type %in% c("soc_eng", "soc_eng_core") ~ "Civic Eng.")) %>% 
  mutate(coreDV=case_when(
    type %in% c("demo_core", "pol_low_core", "soc_eng_core") ~ "Regular engagement",
    type %in% c("demo", "pol_low", "soc_eng") ~ "Engagement")) %>% 
  mutate(typeDV=factor(typeDV, levels = c("Demonstrations", "Political Eng.", "Civic Eng."))) %>% 
  mutate(term=fct_rev(factor(term, levels=c("Threats", "Egotropic health threat", "Sociotropic health threat",
                                    "Egotropic economic threat", "Sociotropic economic threat", "Ideology",
                                    "Extreme Left", "Extreme Right")))) %>% 
  mutate(y2=case_when(term=="Egotropic health threat" ~1,
    term=="Sociotropic health threat" ~ 2,
    term=="Egotropic economic threat" ~ 3,
    term=="Sociotropic economic threat" ~ 4,
    term=="Extreme Left" ~ 5,
    term=="Extreme Right" ~ 6)) %>% 
  mutate_at(vars(odds, odds.outter.upper, odds.outter.lower, odds.inner.upper, odds.inner.lower, p.value, probability, prob.outter.upper, prob.outter.lower, prob.inner.upper, prob.inner.lower), 
            ~ case_when(term %in% c("Threats", "Ideology") ~ as.numeric(NA),
                      TRUE ~ as.numeric(.)))

plot_dat$new_term <- ifelse(plot_dat$typeDV=="Civic Eng.", 
                            as.character(as.factor(plot_dat$term)),
                            ifelse(plot_dat$typeDV=="Political Eng.",             paste0(as.character(as.factor(plot_dat$term)), "_pol"), paste0(as.character(as.factor(plot_dat$term)), "_demo")))

ggplot(plot_dat, aes(y=term, color=typeDV)) +
  geom_vline(xintercept = 1, linetype="dashed", color="gray60") +
  geom_point(aes(x=odds, shape=typeDV), position=position_dodge(width=0.8), size=2) +
  geom_linerange(aes(xmin=odds.outter.lower, xmax=odds.outter.upper, 
                     group=typeDV), size=0.5, position=position_dodge(width=0.8), key_glyph = "path") +
  geom_linerange(aes(xmin=odds.inner.lower, xmax=odds.inner.upper, 
                     group=typeDV), size=1.1, position=position_dodge(width=0.8), key_glyph = "path") +
  facet_grid(~coreDV) +
  xlab("Odds Ratio") +
  scale_color_manual(name="", values = c("#356e38", "#66d46d", "#355a94"),
                     guide=guide_legend(reverse = TRUE)) +
  scale_shape_discrete(name="", guide=guide_legend(reverse = TRUE)) +
  theme_bw() +
  theme(legend.title=element_blank(), 
        legend.position="bottom",
        legend.key.width = unit(1,"cm"),
        axis.title.y = element_blank(),
        axis.text.y = element_text(face = c(rep('plain',2), 'bold', rep('plain',4), 'bold')),
        axis.ticks.y = element_blank())

ggsave(plot=last_plot(),
       filename="figure4_odds.png",
       path=paste0(path, "Revisions/figures/"),
       width=7,
       height=5,
       dpi=400)

```

# Appendix A.3

## Figure 1

```{r, warning=FALSE, echo=FALSE, message=FALSE, include=FALSE}

png(file="Factor_scree_plot.png", width=600, height=350)

fa.parallel(subset(dat, select = c(help_neighborhood:stg_else_pol)), fa = 'fa', cor="poly") 

dev.off()

```

## Figure 2

```{r, warning=FALSE, echo=FALSE, message=FALSE}

one_fac <- fa(subset(dat, select = c(help_neighborhood:stg_else_pol)), nfactors = 1, cor="poly")

png(file="one_factor.png", width=400, height=400)

fa.diagram(one_fac)

dev.off()

```

## Figure 3

```{r, warning=FALSE, echo=FALSE, message=FALSE}

two_fac <- fa(subset(dat, select = c(help_neighborhood:stg_else_pol)), nfactors = 2, cor="poly")

png(file="two_factor.png", width=400, height=400)

fa.diagram(two_fac)

dev.off()

```

## Figure 4

```{r, warning=FALSE, echo=FALSE, message=FALSE}

two_fac <- fa(subset(dat, select = c(help_neighborhood:stg_else_pol)), nfactors = 2, cor="poly", rotate="varimax")

png(file="two_factor_rotated.png", width=400, height=400)

fa.diagram(two_fac)

dev.off()

```

# Appendix A.4

## Country specific figures

```{r, warning=FALSE, echo=FALSE, message=FALSE, include=FALSE}

# To extract the coefficient:

models <- data.frame(type = c("demo", "pol_low", "soc_eng",
                             "demo_core", "pol_low_core", "soc_eng_core"))

for (i in unique(eff_sample$Qcountry)) {
  
  controls <- c("age", "age2", "gender", "children", "edu", "econ_sit", "extr_left",
                "extr_right", "pol_intr", "tr_nat", "gov_dissat")
  threats <- c("eco_ind_dummy", "health_ind_dummy", "eco_soc_dummy", "health_soc_dummy")
  
  demo <- as.formula(paste0("demo ~", paste(controls, "+", threats, collapse = "+")))
  pol_low <- as.formula(paste0("pol_low ~", paste(controls, "+", threats, collapse = "+")))
  soc_eng <- as.formula(paste0("soc_eng ~", paste(controls, "+", threats, collapse = "+")))
  demo_core <- as.formula(paste0("demo_core ~", paste(controls, "+", threats, collapse = "+")))
  pol_low_core <- as.formula(paste0("pol_low_core ~", paste(controls, "+", threats, collapse = "+")))
  soc_eng_core <- as.formula(paste0("soc_eng_core ~", paste(controls, "+", threats, collapse = "+")))
  
  mod1 <- glm(formula = demo,
              data = subset(eff_sample, Qcountry==i),
              weights=subset(eff_sample$weight, eff_sample$Qcountry==i))
  mod2 <- glm(formula = pol_low,
              data = subset(eff_sample, Qcountry==i),
              weights=subset(eff_sample$weight, eff_sample$Qcountry==i))
  mod3 <- glm(formula = soc_eng,
              data = subset(eff_sample, Qcountry==i),
              weights=subset(eff_sample$weight, eff_sample$Qcountry==i))
  mod4 <- glm(formula = demo_core,
              data = subset(eff_sample, Qcountry==i),
              weights=subset(eff_sample$weight, eff_sample$Qcountry==i))
  mod5 <- glm(formula = pol_low_core,
              data = subset(eff_sample, Qcountry==i),
              weights=subset(eff_sample$weight, eff_sample$Qcountry==i))
  mod6 <- glm(formula = soc_eng_core,
              data = subset(eff_sample, Qcountry==i),
              weights=subset(eff_sample$weight, eff_sample$Qcountry==i))
  
  mod1_res <- tidy(mod1)
  mod1_res <- mod1_res %>% 
    mutate(Qcountry=i,
           type="demo")
  mod_1_ci <- as.data.frame(confint(mod1))
  mod_1_ci$term <- rownames(mod_1_ci)
  mod1_res <- merge(mod_1_ci, mod1_res, by="term", all=TRUE)
  mod_1_ci <- as.data.frame(confint(mod1, level=0.9))
  mod_1_ci$term <- rownames(mod_1_ci)
  mod1_res <- merge(mod_1_ci, mod1_res, by="term", all=TRUE)

  mod2_res <- tidy(mod2)
  mod2_res <- mod2_res %>% 
    mutate(Qcountry=i,
           type="pol_low")
  mod_2_ci <- as.data.frame(confint(mod2))
  mod_2_ci$term <- rownames(mod_2_ci)
  mod2_res <- merge(mod_2_ci, mod2_res, by="term", all=TRUE)
  mod_2_ci <- as.data.frame(confint(mod2, level=0.9))
  mod_2_ci$term <- rownames(mod_2_ci)
  mod2_res <- merge(mod_2_ci, mod2_res, by="term", all=TRUE)

  mod3_res <- tidy(mod3)
  mod3_res <- mod3_res %>% 
    mutate(Qcountry=i,
           type="soc_eng")
  mod_3_ci <- as.data.frame(confint(mod3))
  mod_3_ci$term <- rownames(mod_3_ci)
  mod3_res <- merge(mod_3_ci, mod3_res, by="term", all=TRUE)
  mod_3_ci <- as.data.frame(confint(mod3, level=0.9))
  mod_3_ci$term <- rownames(mod_3_ci)
  mod3_res <- merge(mod_3_ci, mod3_res, by="term", all=TRUE)

  mod4_res <- tidy(mod4)
  mod4_res <- mod4_res %>% 
    mutate(Qcountry=i,
           type="demo_core")
  mod_4_ci <- as.data.frame(confint(mod4))
  mod_4_ci$term <- rownames(mod_4_ci)
  mod4_res <- merge(mod_4_ci, mod4_res, by="term", all=TRUE)
  mod_4_ci <- as.data.frame(confint(mod4, level=0.9))
  mod_4_ci$term <- rownames(mod_4_ci)
  mod4_res <- merge(mod_4_ci, mod4_res, by="term", all=TRUE)

  mod5_res <- tidy(mod5)
  mod5_res <- mod5_res %>% 
    mutate(Qcountry=i,
           type="pol_low_core")
  mod_5_ci <- as.data.frame(confint(mod5))
  mod_5_ci$term <- rownames(mod_5_ci)
  mod5_res <- merge(mod_5_ci, mod5_res, by="term", all=TRUE)
  mod_5_ci <- as.data.frame(confint(mod5, level=0.9))
  mod_5_ci$term <- rownames(mod_5_ci)
  mod5_res <- merge(mod_5_ci, mod5_res, by="term", all=TRUE)

  mod6_res <- tidy(mod6)
  mod6_res <- mod6_res %>% 
    mutate(Qcountry=i,
           type="soc_eng_core")
  mod_6_ci <- as.data.frame(confint(mod6))
  mod_6_ci$term <- rownames(mod_6_ci)
  mod6_res <- merge(mod_6_ci, mod6_res, by="term", all=TRUE)
  mod_6_ci <- as.data.frame(confint(mod6, level=0.9))
  mod_6_ci$term <- rownames(mod_6_ci)
  mod6_res <- merge(mod_6_ci, mod6_res, by="term", all=TRUE)
  
  models <- merge(models, mod1_res, all=TRUE)
  models <- merge(models, mod2_res, all=TRUE)
  models <- merge(models, mod3_res, all=TRUE)
  models <- merge(models, mod4_res, all=TRUE)
  models <- merge(models, mod5_res, all=TRUE)
  models <- merge(models, mod6_res, all=TRUE)
  
  print(paste0("Fitting models for ", i))

}

```

```{r, warning=FALSE, echo=FALSE, message=FALSE}

for (i in unique(eff_sample$Qcountry)) {

temp <- subset(models, Qcountry==i)
  
plot_dat <- temp %>% 
    dplyr::filter(term %in% c("eco_ind_dummy1", "eco_soc_dummy1",
                            "health_ind_dummy1", "health_soc_dummy1",
                            "extr_left1", "extr_right1", "age", "age2")) %>% 
  mutate(odds = exp(estimate),
         odds.outter.upper = exp(`2.5 %`),
         odds.outter.lower = exp(`97.5 %`),
         odds.inner.upper = exp(`5 %`),
         odds.inner.lower = exp(`95 %`),
         p.value=as.factor(ifelse(p.value<=0.05, 1, 0)),
         probability = odds/(1+odds),
         prob.outter.upper = odds.outter.upper/(1+odds.outter.upper),
         prob.outter.lower = odds.outter.lower/(1+odds.outter.lower),
         prob.inner.upper = odds.inner.upper/(1+odds.inner.upper),
         prob.inner.lower = odds.inner.lower/(1+odds.inner.lower)) %>% 
  mutate(term=case_when(term=="extr_right1" ~ "Extreme Right",
                        term=="extr_left1" ~ "Extreme Left",
                        term %in% c("eco_ind_dummy1", 
                                    "eco_threat_indiv") ~ "Egotropic economic threat",
                        term %in% c("health_ind_dummy1",
                                    "health_threat_indiv") ~ "Egotropic health threat",
         term=="eco_soc_dummy1" ~ "Sociotropic economic threat",
         term=="health_soc_dummy1" ~ "Sociotropic health threat",
         term=="age" ~ "Threats",
         term=="age2" ~ "Ideology")) %>% 
  mutate(typeDV=case_when(
    type %in% c("demo", "demo_core") ~ "Demonstrations",
    type %in% c("pol_low", "pol_low_core") ~ "Political Eng.",
    type %in% c("soc_eng", "soc_eng_core") ~ "Civic Eng.")) %>% 
  mutate(coreDV=case_when(
    type %in% c("demo_core", "pol_low_core", "soc_eng_core") ~ "Regular engagement",
    type %in% c("demo", "pol_low", "soc_eng") ~ "Engagement")) %>% 
  mutate(typeDV=factor(typeDV, levels = c("Demonstrations", "Political Eng.", "Civic Eng."))) %>% 
  mutate(term=fct_rev(factor(term, levels=c("Threats", "Egotropic health threat", "Sociotropic health threat",
                                    "Egotropic economic threat", "Sociotropic economic threat", "Ideology",
                                    "Extreme Left", "Extreme Right")))) %>% 
  mutate(y2=case_when(term=="Egotropic health threat" ~1,
    term=="Sociotropic health threat" ~ 2,
    term=="Egotropic economic threat" ~ 3,
    term=="Sociotropic economic threat" ~ 4,
    term=="Extreme Left" ~ 5,
    term=="Extreme Right" ~ 6)) %>% 
  mutate_at(vars(odds, odds.outter.upper, odds.outter.lower, odds.inner.upper, odds.inner.lower, p.value, probability, prob.outter.upper, prob.outter.lower, prob.inner.upper, prob.inner.lower), 
            ~ case_when(term %in% c("Threats", "Ideology") ~ as.numeric(NA),
                      TRUE ~ as.numeric(.)))

plot_dat$new_term <- ifelse(plot_dat$typeDV=="Civic Eng.", 
                            as.character(as.factor(plot_dat$term)),
                            ifelse(plot_dat$typeDV=="Political Eng.",             paste0(as.character(as.factor(plot_dat$term)), "_pol"), paste0(as.character(as.factor(plot_dat$term)), "_demo")))

ggplot(plot_dat, aes(y=term, color=typeDV)) +
  geom_vline(xintercept = 1, linetype="dashed", color="gray60") +
  geom_point(aes(x=odds, shape=typeDV), position=position_dodge(width=0.8), size=2) +
  geom_linerange(aes(xmin=odds.outter.lower, xmax=odds.outter.upper, 
                     group=typeDV), size=0.5, position=position_dodge(width=0.8), key_glyph = "path") +
  geom_linerange(aes(xmin=odds.inner.lower, xmax=odds.inner.upper, 
                     group=typeDV), size=1.1, position=position_dodge(width=0.8), key_glyph = "path") +
  facet_grid(~coreDV) +
  xlab("Odds Ratio") +
  scale_color_manual(name="", values = c("#356e38", "#66d46d", "#355a94"),
                     guide=guide_legend(reverse = TRUE)) +
  scale_shape_discrete(name="", guide=guide_legend(reverse = TRUE)) +
  theme_bw() +
  theme(legend.title=element_blank(), 
        legend.position="bottom",
        legend.key.width = unit(1,"cm"),
        axis.title.y = element_blank(),
        axis.text.y = element_text(face = c(rep('plain',2), 'bold', rep('plain',4), 'bold')),
        axis.ticks.y = element_blank())

ggsave(plot=last_plot(),
       filename=paste0(as.character(i), "_odds.png"),
       path=paste0(path, "Revisions/figures/"),
       width=7,
       height=5,
       dpi=400)


  print(paste0("Preparing the figure for ", i))

}
```

## Models for all countries

```{r}
mod1 <- glm.cluster(formula = demo ~ age + age2 + gender + children + edu + econ_sit + extr_left + extr_right + pol_intr + tr_nat + gov_dissat + eco_ind_dummy + health_ind_dummy + eco_soc_dummy + health_soc_dummy,
              data = eff_sample,
              weights=eff_sample$weight, cluster="Qcountry")
mod2 <- glm.cluster(formula = pol_low ~ age + age2 + gender + children + edu + econ_sit + extr_left + extr_right + pol_intr + tr_nat + gov_dissat + eco_ind_dummy + health_ind_dummy + eco_soc_dummy + health_soc_dummy,
              data = eff_sample,
              weights=eff_sample$weight, cluster="Qcountry")
mod3 <- glm.cluster(formula = soc_eng ~ age + age2 + gender + children + edu + econ_sit + extr_left + extr_right + pol_intr + tr_nat + gov_dissat + eco_ind_dummy + health_ind_dummy + eco_soc_dummy + health_soc_dummy,
              data = eff_sample,
              weights=eff_sample$weight, cluster="Qcountry")
mod4 <- glm.cluster(formula = demo_core ~ age + age2 + gender + children + edu + econ_sit + extr_left + extr_right + pol_intr + tr_nat + gov_dissat + eco_ind_dummy + health_ind_dummy + eco_soc_dummy + health_soc_dummy,
              data = eff_sample,
              weights=eff_sample$weight, cluster="Qcountry")
mod5 <- glm.cluster(formula = pol_low_core ~ age + age2 + gender + children + edu + econ_sit + extr_left + extr_right + pol_intr + tr_nat + gov_dissat + eco_ind_dummy + health_ind_dummy + eco_soc_dummy + health_soc_dummy,
              data = eff_sample,
              weights=eff_sample$weight, cluster="Qcountry")
mod6 <- glm.cluster(formula = soc_eng_core ~ age + age2 + gender + children + edu + econ_sit + extr_left + extr_right + pol_intr + tr_nat + gov_dissat + eco_ind_dummy + health_ind_dummy + eco_soc_dummy + health_soc_dummy,
              data = eff_sample,
              weights=eff_sample$weight, cluster="Qcountry")
```



```{r, results='asis'}

htmlreg(l=list(mod1, mod2, mod3, mod4, mod5, mod6),
          custom.model.names=c("Demonstration", "Political", "Civic", "Demonstration - core", "Political -core", "Civic -core"),
        custom.coef.names = c("intercept", "age", "age^2", "gender", "children", "education", "economic situation", "extreme left", "extreme right", "political interest", "trust in government", "government dissatisfaction", "indiv. economic threat", "indiv. health threat", "soc. economic threat", "soc. health threat"),
          star.symbol = "\\*", doctype = FALSE, center = FALSE, caption = "")
```


## Country specific tables 



### models for for UK

```{r}
mod1 <- glm(formula = demo ~ age + age2 + gender + children + edu + econ_sit + extr_left + extr_right + pol_intr + tr_nat + gov_dissat + eco_ind_dummy + health_ind_dummy + eco_soc_dummy + health_soc_dummy,
              data = eff_sample, subset=(Qcountry=="United Kingdom"),
              weights=eff_sample$weight)

mod2 <- glm(formula = pol_low ~ age + age2 + gender + children + edu + econ_sit + extr_left + extr_right + pol_intr + tr_nat + gov_dissat + eco_ind_dummy + health_ind_dummy + eco_soc_dummy + health_soc_dummy,
              data = eff_sample, subset=(Qcountry=="United Kingdom"),
              weights=eff_sample$weight)

mod3 <- glm(formula = soc_eng ~ age + age2 + gender + children + edu + econ_sit + extr_left + extr_right + pol_intr + tr_nat + gov_dissat + eco_ind_dummy + health_ind_dummy + eco_soc_dummy + health_soc_dummy,
              data = eff_sample, subset=(Qcountry=="United Kingdom"),
              weights=eff_sample$weight)

mod4 <- glm(formula = demo_core ~ age + age2 + gender + children + edu + econ_sit + extr_left + extr_right + pol_intr + tr_nat + gov_dissat + eco_ind_dummy + health_ind_dummy + eco_soc_dummy + health_soc_dummy,
              data = eff_sample, subset=(Qcountry=="United Kingdom"),
              weights=eff_sample$weight)

mod5 <- glm(formula = pol_low_core ~ age + age2 + gender + children + edu + econ_sit + extr_left + extr_right + pol_intr + tr_nat + gov_dissat + eco_ind_dummy + health_ind_dummy + eco_soc_dummy + health_soc_dummy,
              data = eff_sample, subset=(Qcountry=="United Kingdom"),
              weights=eff_sample$weight)

mod6 <- glm(formula = soc_eng_core ~ age + age2 + gender + children + edu + econ_sit + extr_left + extr_right + pol_intr + tr_nat + gov_dissat + eco_ind_dummy + health_ind_dummy + eco_soc_dummy + health_soc_dummy,
              data = eff_sample, subset=(Qcountry=="United Kingdom"),
              weights=eff_sample$weight)
```


```{r, results='asis'}

htmlreg(l=list(mod1, mod2, mod3, mod4, mod5, mod6),
          custom.model.names=c("Demonstration", "Political", "Civic", "Demonstration - core", "Political -core", "Civic -core"),
        custom.coef.names = c("intercept", "age", "age^2", "gender", "children", "education", "economic situation", "extreme left", "extreme right", "political interest", "trust in government", "government dissatisfaction", "indiv. economic threat", "indiv. health threat", "soc. economic threat", "soc. health threat"),
          star.symbol = "\\*", doctype = FALSE, center = FALSE, caption = "")
```


### models for for Sweden

```{r}
mod1 <- glm(formula = demo ~ age + age2 + gender + children + edu + econ_sit + extr_left + extr_right + pol_intr + tr_nat + gov_dissat + eco_ind_dummy + health_ind_dummy + eco_soc_dummy + health_soc_dummy,
              data = eff_sample, subset=(Qcountry=="Sweden"),
              weights=eff_sample$weight)

mod2 <- glm(formula = pol_low ~ age + age2 + gender + children + edu + econ_sit + extr_left + extr_right + pol_intr + tr_nat + gov_dissat + eco_ind_dummy + health_ind_dummy + eco_soc_dummy + health_soc_dummy,
              data = eff_sample, subset=(Qcountry=="Sweden"),
              weights=eff_sample$weight)

mod3 <- glm(formula = soc_eng ~ age + age2 + gender + children + edu + econ_sit + extr_left + extr_right + pol_intr + tr_nat + gov_dissat + eco_ind_dummy + health_ind_dummy + eco_soc_dummy + health_soc_dummy,
              data = eff_sample, subset=(Qcountry=="Sweden"),
              weights=eff_sample$weight)

mod4 <- glm(formula = demo_core ~ age + age2 + gender + children + edu + econ_sit + extr_left + extr_right + pol_intr + tr_nat + gov_dissat + eco_ind_dummy + health_ind_dummy + eco_soc_dummy + health_soc_dummy,
              data = eff_sample, subset=(Qcountry=="Sweden"),
              weights=eff_sample$weight)

mod5 <- glm(formula = pol_low_core ~ age + age2 + gender + children + edu + econ_sit + extr_left + extr_right + pol_intr + tr_nat + gov_dissat + eco_ind_dummy + health_ind_dummy + eco_soc_dummy + health_soc_dummy,
              data = eff_sample, subset=(Qcountry=="Sweden"),
              weights=eff_sample$weight)

mod6 <- glm(formula = soc_eng_core ~ age + age2 + gender + children + edu + econ_sit + extr_left + extr_right + pol_intr + tr_nat + gov_dissat + eco_ind_dummy + health_ind_dummy + eco_soc_dummy + health_soc_dummy,
              data = eff_sample, subset=(Qcountry=="Sweden"),
              weights=eff_sample$weight)
```


```{r, results='asis'}

htmlreg(l=list(mod1, mod2, mod3, mod4, mod5, mod6),
          custom.model.names=c("Demonstration", "Political", "Civic", "Demonstration - core", "Political -core", "Civic -core"),
        custom.coef.names = c("intercept", "age", "age^2", "gender", "children", "education", "economic situation", "extreme left", "extreme right", "political interest", "trust in government", "government dissatisfaction", "indiv. economic threat", "indiv. health threat", "soc. economic threat", "soc. health threat"),
          star.symbol = "\\*", doctype = FALSE, center = FALSE, caption = "")
```

### models for the Netherlands

```{r}
mod1 <- glm(formula = demo ~ age + age2 + gender + children + edu + econ_sit + extr_left + extr_right + pol_intr + tr_nat + gov_dissat + eco_ind_dummy + health_ind_dummy + eco_soc_dummy + health_soc_dummy,
              data = eff_sample, subset=(Qcountry=="Netherlands"),
              weights=eff_sample$weight)

mod2 <- glm(formula = pol_low ~ age + age2 + gender + children + edu + econ_sit + extr_left + extr_right + pol_intr + tr_nat + gov_dissat + eco_ind_dummy + health_ind_dummy + eco_soc_dummy + health_soc_dummy,
              data = eff_sample, subset=(Qcountry=="Netherlands"),
              weights=eff_sample$weight)

mod3 <- glm(formula = soc_eng ~ age + age2 + gender + children + edu + econ_sit + extr_left + extr_right + pol_intr + tr_nat + gov_dissat + eco_ind_dummy + health_ind_dummy + eco_soc_dummy + health_soc_dummy,
              data = eff_sample, subset=(Qcountry=="Netherlands"),
              weights=eff_sample$weight)

mod4 <- glm(formula = demo_core ~ age + age2 + gender + children + edu + econ_sit + extr_left + extr_right + pol_intr + tr_nat + gov_dissat + eco_ind_dummy + health_ind_dummy + eco_soc_dummy + health_soc_dummy,
              data = eff_sample, subset=(Qcountry=="Netherlands"),
              weights=eff_sample$weight)

mod5 <- glm(formula = pol_low_core ~ age + age2 + gender + children + edu + econ_sit + extr_left + extr_right + pol_intr + tr_nat + gov_dissat + eco_ind_dummy + health_ind_dummy + eco_soc_dummy + health_soc_dummy,
              data = eff_sample, subset=(Qcountry=="Netherlands"),
              weights=eff_sample$weight)

mod6 <- glm(formula = soc_eng_core ~ age + age2 + gender + children + edu + econ_sit + extr_left + extr_right + pol_intr + tr_nat + gov_dissat + eco_ind_dummy + health_ind_dummy + eco_soc_dummy + health_soc_dummy,
              data = eff_sample, subset=(Qcountry=="Netherlands"),
              weights=eff_sample$weight)
```



```{r, results='asis'}

htmlreg(l=list(mod1, mod2, mod3, mod4, mod5, mod6),
          custom.model.names=c("Demonstration", "Political", "Civic", "Demonstration - core", "Political -core", "Civic -core"),
        custom.coef.names = c("intercept", "age", "age^2", "gender", "children", "education", "economic situation", "extreme left", "extreme right", "political interest", "trust in government", "government dissatisfaction", "indiv. economic threat", "indiv. health threat", "soc. economic threat", "soc. health threat"),
          star.symbol = "\\*", doctype = FALSE, center = FALSE, caption = "")
```


### models for Italy

```{r}
mod1 <- glm(formula = demo ~ age + age2 + gender + children + edu + econ_sit + extr_left + extr_right + pol_intr + tr_nat + gov_dissat + eco_ind_dummy + health_ind_dummy + eco_soc_dummy + health_soc_dummy,
              data = eff_sample, subset=(Qcountry=="Italy"),
              weights=eff_sample$weight)

mod2 <- glm(formula = pol_low ~ age + age2 + gender + children + edu + econ_sit + extr_left + extr_right + pol_intr + tr_nat + gov_dissat + eco_ind_dummy + health_ind_dummy + eco_soc_dummy + health_soc_dummy,
              data = eff_sample, subset=(Qcountry=="Italy"),
              weights=eff_sample$weight)

mod3 <- glm(formula = soc_eng ~ age + age2 + gender + children + edu + econ_sit + extr_left + extr_right + pol_intr + tr_nat + gov_dissat + eco_ind_dummy + health_ind_dummy + eco_soc_dummy + health_soc_dummy,
              data = eff_sample, subset=(Qcountry=="Italy"),
              weights=eff_sample$weight)

mod4 <- glm(formula = demo_core ~ age + age2 + gender + children + edu + econ_sit + extr_left + extr_right + pol_intr + tr_nat + gov_dissat + eco_ind_dummy + health_ind_dummy + eco_soc_dummy + health_soc_dummy,
              data = eff_sample, subset=(Qcountry=="Italy"),
              weights=eff_sample$weight)

mod5 <- glm(formula = pol_low_core ~ age + age2 + gender + children + edu + econ_sit + extr_left + extr_right + pol_intr + tr_nat + gov_dissat + eco_ind_dummy + health_ind_dummy + eco_soc_dummy + health_soc_dummy,
              data = eff_sample, subset=(Qcountry=="Italy"),
              weights=eff_sample$weight)

mod6 <- glm(formula = soc_eng_core ~ age + age2 + gender + children + edu + econ_sit + extr_left + extr_right + pol_intr + tr_nat + gov_dissat + eco_ind_dummy + health_ind_dummy + eco_soc_dummy + health_soc_dummy,
              data = eff_sample, subset=(Qcountry=="Italy"),
              weights=eff_sample$weight)
```



```{r, results='asis'}

htmlreg(l=list(mod1, mod2, mod3, mod4, mod5, mod6),
          custom.model.names=c("Demonstration", "Political", "Civic", "Demonstration - core", "Political -core", "Civic -core"),
        custom.coef.names = c("intercept", "age", "age^2", "gender", "children", "education", "economic situation", "extreme left", "extreme right", "political interest", "trust in government", "government dissatisfaction", "indiv. economic threat", "indiv. health threat", "soc. economic threat", "soc. health threat"),
          star.symbol = "\\*", doctype = FALSE, center = FALSE, caption = "")
```


### models for Germany

```{r}
mod1 <- glm(formula = demo ~ age + age2 + gender + children + edu + econ_sit + extr_left + extr_right + pol_intr + tr_nat + gov_dissat + eco_ind_dummy + health_ind_dummy + eco_soc_dummy + health_soc_dummy,
              data = eff_sample, subset=(Qcountry=="Germany"),
              weights=eff_sample$weight)

mod2 <- glm(formula = pol_low ~ age + age2 + gender + children + edu + econ_sit + extr_left + extr_right + pol_intr + tr_nat + gov_dissat + eco_ind_dummy + health_ind_dummy + eco_soc_dummy + health_soc_dummy,
              data = eff_sample, subset=(Qcountry=="Germany"),
              weights=eff_sample$weight)

mod3 <- glm(formula = soc_eng ~ age + age2 + gender + children + edu + econ_sit + extr_left + extr_right + pol_intr + tr_nat + gov_dissat + eco_ind_dummy + health_ind_dummy + eco_soc_dummy + health_soc_dummy,
              data = eff_sample, subset=(Qcountry=="Germany"),
              weights=eff_sample$weight)

mod4 <- glm(formula = demo_core ~ age + age2 + gender + children + edu + econ_sit + extr_left + extr_right + pol_intr + tr_nat + gov_dissat + eco_ind_dummy + health_ind_dummy + eco_soc_dummy + health_soc_dummy,
              data = eff_sample, subset=(Qcountry=="Germany"),
              weights=eff_sample$weight)

mod5 <- glm(formula = pol_low_core ~ age + age2 + gender + children + edu + econ_sit + extr_left + extr_right + pol_intr + tr_nat + gov_dissat + eco_ind_dummy + health_ind_dummy + eco_soc_dummy + health_soc_dummy,
              data = eff_sample, subset=(Qcountry=="Germany"),
              weights=eff_sample$weight)

mod6 <- glm(formula = soc_eng_core ~ age + age2 + gender + children + edu + econ_sit + extr_left + extr_right + pol_intr + tr_nat + gov_dissat + eco_ind_dummy + health_ind_dummy + eco_soc_dummy + health_soc_dummy,
              data = eff_sample, subset=(Qcountry=="Germany"),
              weights=eff_sample$weight)
```


```{r, results='asis'}

htmlreg(l=list(mod1, mod2, mod3, mod4, mod5, mod6),
          custom.model.names=c("Demonstration", "Political", "Civic", "Demonstration - core", "Political -core", "Civic -core"),
        custom.coef.names = c("intercept", "age", "age^2", "gender", "children", "education", "economic situation", "extreme left", "extreme right", "political interest", "trust in government", "government dissatisfaction", "indiv. economic threat", "indiv. health threat", "soc. economic threat", "soc. health threat"),
          star.symbol = "\\*", doctype = FALSE, center = FALSE, caption = "")
```

### models for France

```{r}
mod1 <- glm(formula = demo ~ age + age2 + gender + children + edu + econ_sit + extr_left + extr_right + pol_intr + tr_nat + gov_dissat + eco_ind_dummy + health_ind_dummy + eco_soc_dummy + health_soc_dummy,
              data = eff_sample, subset=(Qcountry=="France"),
              weights=eff_sample$weight)

mod2 <- glm(formula = pol_low ~ age + age2 + gender + children + edu + econ_sit + extr_left + extr_right + pol_intr + tr_nat + gov_dissat + eco_ind_dummy + health_ind_dummy + eco_soc_dummy + health_soc_dummy,
              data = eff_sample, subset=(Qcountry=="France"),
              weights=eff_sample$weight)

mod3 <- glm(formula = soc_eng ~ age + age2 + gender + children + edu + econ_sit + extr_left + extr_right + pol_intr + tr_nat + gov_dissat + eco_ind_dummy + health_ind_dummy + eco_soc_dummy + health_soc_dummy,
              data = eff_sample, subset=(Qcountry=="France"),
              weights=eff_sample$weight)

mod4 <- glm(formula = demo_core ~ age + age2 + gender + children + edu + econ_sit + extr_left + extr_right + pol_intr + tr_nat + gov_dissat + eco_ind_dummy + health_ind_dummy + eco_soc_dummy + health_soc_dummy,
              data = eff_sample, subset=(Qcountry=="France"),
              weights=eff_sample$weight)

mod5 <- glm(formula = pol_low_core ~ age + age2 + gender + children + edu + econ_sit + extr_left + extr_right + pol_intr + tr_nat + gov_dissat + eco_ind_dummy + health_ind_dummy + eco_soc_dummy + health_soc_dummy,
              data = eff_sample, subset=(Qcountry=="France"),
              weights=eff_sample$weight)

mod6 <- glm(formula = soc_eng_core ~ age + age2 + gender + children + edu + econ_sit + extr_left + extr_right + pol_intr + tr_nat + gov_dissat + eco_ind_dummy + health_ind_dummy + eco_soc_dummy + health_soc_dummy,
              data = eff_sample, subset=(Qcountry=="France"),
              weights=eff_sample$weight)
```


```{r, results='asis'}

htmlreg(l=list(mod1, mod2, mod3, mod4, mod5, mod6),
          custom.model.names=c("Demonstration", "Political", "Civic", "Demonstration - core", "Political -core", "Civic -core"),
        custom.coef.names = c("intercept", "age", "age^2", "gender", "children", "education", "economic situation", "extreme left", "extreme right", "political interest", "trust in government", "government dissatisfaction", "indiv. economic threat", "indiv. health threat", "soc. economic threat", "soc. health threat"),
          star.symbol = "\\*", doctype = FALSE, center = FALSE, caption = "Models for France")
```

### models for Spain

```{r}
mod1 <- glm(formula = demo ~ age + age2 + gender + children + edu + econ_sit + extr_left + extr_right + pol_intr + tr_nat + gov_dissat + eco_ind_dummy + health_ind_dummy + eco_soc_dummy + health_soc_dummy,
              data = eff_sample, subset=(Qcountry=="Spain"),
              weights=eff_sample$weight)

mod2 <- glm(formula = pol_low ~ age + age2 + gender + children + edu + econ_sit + extr_left + extr_right + pol_intr + tr_nat + gov_dissat + eco_ind_dummy + health_ind_dummy + eco_soc_dummy + health_soc_dummy,
              data = eff_sample, subset=(Qcountry=="Spain"),
              weights=eff_sample$weight)

mod3 <- glm(formula = soc_eng ~ age + age2 + gender + children + edu + econ_sit + extr_left + extr_right + pol_intr + tr_nat + gov_dissat + eco_ind_dummy + health_ind_dummy + eco_soc_dummy + health_soc_dummy,
              data = eff_sample, subset=(Qcountry=="Spain"),
              weights=eff_sample$weight)

mod4 <- glm(formula = demo_core ~ age + age2 + gender + children + edu + econ_sit + extr_left + extr_right + pol_intr + tr_nat + gov_dissat + eco_ind_dummy + health_ind_dummy + eco_soc_dummy + health_soc_dummy,
              data = eff_sample, subset=(Qcountry=="Spain"),
              weights=eff_sample$weight)

mod5 <- glm(formula = pol_low_core ~ age + age2 + gender + children + edu + econ_sit + extr_left + extr_right + pol_intr + tr_nat + gov_dissat + eco_ind_dummy + health_ind_dummy + eco_soc_dummy + health_soc_dummy,
              data = eff_sample, subset=(Qcountry=="Spain"),
              weights=eff_sample$weight)

mod6 <- glm(formula = soc_eng_core ~ age + age2 + gender + children + edu + econ_sit + extr_left + extr_right + pol_intr + tr_nat + gov_dissat + eco_ind_dummy + health_ind_dummy + eco_soc_dummy + health_soc_dummy,
              data = eff_sample, subset=(Qcountry=="Spain"),
              weights=eff_sample$weight)
```



```{r, results='asis'}

htmlreg(l=list(mod1, mod2, mod3, mod4, mod5, mod6),
          custom.model.names=c("Demonstration", "Political", "Civic", "Demonstration - core", "Political -core", "Civic -core"),
        custom.coef.names = c("intercept", "age", "age^2", "gender", "children", "education", "economic situation", "extreme left", "extreme right", "political interest", "trust in government", "government dissatisfaction", "indiv. economic threat", "indiv. health threat", "soc. economic threat", "soc. health threat"),
          star.symbol = "\\*", doctype = FALSE, center = FALSE, caption = "Models for Spain")
```

# Appendix A.5

## Figure 1

```{r, warning=FALSE, echo=FALSE, message=FALSE, fig.width=9}

DV_dummy <- dat %>% 
  dplyr::select(demo, pol_low, soc_eng, demo_core, pol_low_core, soc_eng_core, "Qcountry", "weight") %>% 
  group_by(Qcountry) %>% 
  mutate_at(vars(demo, pol_low, soc_eng, demo_core, pol_low_core, soc_eng_core), 
            ~as.numeric(as.character(.))*weight) %>% 
  mutate_at(vars(demo, pol_low, soc_eng, demo_core, pol_low_core, soc_eng_core, weight), ~sum(., na.rm = TRUE)) %>% 
  unique(.) %>% 
  mutate_at(vars(demo, pol_low, soc_eng, demo_core, pol_low_core, soc_eng_core), ~.*100/weight) %>% 
  dplyr::select(-weight) %>% 
  pivot_longer(cols=c(demo, pol_low, soc_eng, demo_core, pol_low_core, soc_eng_core), names_to="indicator", values_to="value") %>% 
  mutate(type=ifelse(grepl("_core", indicator), "Regular participants", "Overall")) %>% 
  mutate(type=factor(type, levels=c("Overall", "Regular participants"))) %>% 
  mutate(form = case_when(
    grepl("demo", indicator) ~ "Demonstrations",
    grepl("pol_low", indicator) ~ "Political engagement",
    grepl("soc_eng", indicator) ~ "Civic engagement")) %>% 
  mutate(form=factor(form, levels=c("Civic engagement", "Political engagement", "Demonstrations"))) %>% 
  dplyr::rename(shares=value) %>% 
  mutate(order=case_when(
    Qcountry=="France" ~ 6,
Qcountry=="Germany" ~ 3,
Qcountry=="Italy" ~ 5,
Qcountry=="Netherlands" ~ 2,
Qcountry=="Spain " ~ 7,
Qcountry=="Sweden" ~ 1,
Qcountry=="United Kingdom" ~ 4))

ggplot(data=DV_dummy[DV_dummy$type=="Overall", ], aes(x=reorder(Qcountry, order), y=shares)) +
  geom_bar(stat="identity", position = "dodge", color="black") +
  facet_wrap(~form) +
  xlab("") + ylab("Share (percentage point)") +
  theme_bw() +
  theme(legend.title=element_blank(), legend.position="bottom",
        axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1))


```

## Table 1

```{r, warning=FALSE, echo=FALSE, message=FALSE, fig.width=10}

DV_overall <- dat %>% 
  dplyr::select("public_demo", "donated_money", 
                "signed_petition", "contacted_pol",
                "help_neighborhood", "posted_stg", 
                "stg_else_pol", "Qcountry", "weight") %>% 
  group_by(Qcountry) %>% 
  mutate_at(vars("public_demo", "donated_money", 
                 "signed_petition", "contacted_pol",
                 "help_neighborhood", "posted_stg", 
                 "stg_else_pol"), ~ifelse(.>0, 1, 0)) %>% 
  mutate_at(vars("public_demo", "donated_money", 
                 "signed_petition", "contacted_pol",
                 "help_neighborhood", "posted_stg", 
                 "stg_else_pol"), ~.*weight) %>% 
  group_by(Qcountry) %>% 
  mutate_at(vars("public_demo", "donated_money", 
                 "signed_petition", "contacted_pol",
                 "help_neighborhood", "posted_stg", 
                 "stg_else_pol", "weight"), ~sum(., na.rm = TRUE)) %>% 
  unique(.) %>% 
  mutate_at(vars("public_demo", "donated_money", 
                 "signed_petition", "contacted_pol",
                 "help_neighborhood", "posted_stg", 
                 "stg_else_pol"), ~.*100/weight) %>% 
  dplyr::select(-weight) %>% 
  pivot_longer(cols=c("public_demo", "donated_money", 
                      "signed_petition", "contacted_pol",
                      "help_neighborhood", "posted_stg", 
                      "stg_else_pol"), names_to="indicator", values_to="value") %>% 
  mutate(indicator=dplyr::case_when(indicator=="contacted_pol" ~ "Contacted a pol.",
                                    indicator=="donated_money" ~  "Donated money",
                                    indicator=="help_neighborhood" ~ "Help in neighborhood",
                                    indicator=="posted_stg" ~  "Posted on the Internet",
                                    indicator=="public_demo" ~  "Public demonstration",
                                    indicator=="signed_petition" ~  "Signed a petition",
                                    indicator=="stg_else_pol" ~ "Other political")) %>% 
  mutate(indicator=factor(indicator, levels = c("Help in neighborhood", 
                                                "Donated money", "Posted on the Internet", 
                                                "Signed a petition", "Contacted a pol.",  
                                                "Public demonstration", "Other political"))) %>% 
  arrange(Qcountry, indicator) %>% 
  mutate(Qcountry=case_when(Qcountry=="UK" ~ "United Kingdom",
                            Qcountry=="NL" ~ "Netherlands",
                            TRUE ~ as.character(Qcountry))) %>% 
  dplyr::rename(overall=value)

DV_core <- dat %>% 
  dplyr::select("public_demo", "donated_money", 
                "signed_petition", "contacted_pol",
                "help_neighborhood", "posted_stg", 
                "stg_else_pol", "Qcountry", "weight") %>% 
  group_by(Qcountry) %>% 
  mutate_at(vars("public_demo", "donated_money", 
                 "signed_petition", "contacted_pol",
                 "help_neighborhood", "posted_stg", 
                 "stg_else_pol"), ~ifelse(.==3, 1, 0)) %>% 
  mutate_at(vars("public_demo", "donated_money", 
                 "signed_petition", "contacted_pol",
                 "help_neighborhood", "posted_stg", 
                 "stg_else_pol"), ~.*weight) %>% 
  group_by(Qcountry) %>% 
  mutate_at(vars("public_demo", "donated_money", 
                 "signed_petition", "contacted_pol",
                 "help_neighborhood", "posted_stg", 
                 "stg_else_pol", "weight"), ~sum(., na.rm = TRUE)) %>% 
  unique(.) %>% 
  mutate_at(vars("public_demo", "donated_money", 
                 "signed_petition", "contacted_pol",
                 "help_neighborhood", "posted_stg", 
                 "stg_else_pol"), ~.*100/weight) %>% 
  dplyr::select(-weight) %>% 
  pivot_longer(cols=c("public_demo", "donated_money", 
                      "signed_petition", "contacted_pol",
                      "help_neighborhood", "posted_stg", 
                      "stg_else_pol"), names_to="indicator", values_to="value") %>% 
  mutate(indicator=dplyr::case_when(indicator=="contacted_pol" ~ "Contacted a pol.",
                                    indicator=="donated_money" ~  "Donated money",
                                    indicator=="help_neighborhood" ~ "Help in neighborhood",
                                    indicator=="posted_stg" ~  "Posted on the Internet",
                                    indicator=="public_demo" ~  "Public demonstration",
                                    indicator=="signed_petition" ~  "Signed a petition",
                                    indicator=="stg_else_pol" ~ "Other political")) %>% 
  mutate(indicator=factor(indicator, levels = c("Help in neighborhood", 
                                                "Donated money", "Posted on the Internet", 
                                                "Signed a petition", "Contacted a pol.",  
                                                "Public demonstration", "Other political"))) %>% 
  arrange(Qcountry, indicator) %>% 
  mutate(Qcountry=case_when(Qcountry=="UK" ~ "United Kingdom",
                            Qcountry=="NL" ~ "Netherlands",
                            TRUE ~ as.character(Qcountry))) %>% 
  dplyr::rename(core=value)

DV_dummy <- merge(DV_overall, DV_core)

rm(DV_overall, DV_core)

kable(DV_dummy %>% 
        mutate(core=round(core, 1),
               overall=round(overall, 1)))

DV_dummy <- DV_dummy %>% 
  ungroup(.) %>% 
  dplyr::select(-Qcountry) %>% 
  group_by(indicator) %>% 
  mutate(overall=mean(overall),
         core=mean(core)) %>% 
  unique(.)
```
